/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dp1.titandevelop.titano.view.prod;

import dp1.titandevelop.titano.bean.Estado;
import dp1.titandevelop.titano.bean.Mensajes;
import dp1.titandevelop.titano.persistent.Maquina;
import dp1.titandevelop.titano.persistent.Proceso;
import dp1.titandevelop.titano.service.MaquinaService;
import dp1.titandevelop.titano.service.ProcesoService;
import dp1.titandevelop.titano.service.ValidacionService;
import dp1.titandevelop.titano.view.MensajeError;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
/**
 *
 * @author Yulian
 */
public class ModulosT_PRO_MaquinaEditar extends javax.swing.JInternalFrame {

    private Maquina aux = new Maquina();
    private  List<Proceso> procesos;
    private  List<Estado> estados;
    private  List<String> listaEstString;
    
    
    
    
    private int findIndex(Maquina a, List<Proceso> lista) {
        int i = 0;
        int idTipo = a.getToProceso().getIdproceso();
        while (i < lista.size()) {
            if (idTipo == lista.get(i).getIdproceso()) {
                break;
            }
            i++;
        }
        return (i + 1);
    }
    //lista de estados
    
    private void addList (List<Proceso> lista,int estado){
        
        int posicion = 0;
         for (int i = 0; i < lista.size(); i++) {
            this.cboxproceso.addItem(lista.get(i).getDescripcion());
        }
        this.cboxproceso.setSelectedIndex(posicion);
        
        
        //para estados
        Estado est = new Estado();
        this.listaEstString = new ArrayList<String>();
        listaEstString = est.getEstadosActivoInactivo();
        this.cboxEstado.setModel(new javax.swing.DefaultComboBoxModel(listaEstString.toArray())); 
        
        for (int i=0; i<listaEstString.size(); i++)
        {
            if (listaEstString.get(i).equals(Estado.devuelveCadena(estado))){
                posicion = i;
            }
        }
        this.cboxEstado.setSelectedIndex(posicion);
    }
    
    ModulosT_PRO_MaquinaEditar(Maquina t) {
        initComponents();
        
        ProcesoService serv = new ProcesoService();
        procesos = serv.buscarProcAll();
        addList(procesos,t.getEstado());
        int index = findIndex(t, procesos);
        this.cboxproceso.setSelectedIndex(index);
        
        this.txtDescripcion.setText(t.getDescripcion());
        this.txtKwh.setText(t.getKwh().toString());
        
        this.aux =t;
        
       ValidacionService vs = new ValidacionService();  
        vs.SLetras(this.txtDescripcion);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtDescripcion = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        cboxEstado = new javax.swing.JComboBox();
        txtKwh = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        cboxproceso = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        btnGuardar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();

        setResizable(true);
        setTitle("Edición de Máquina");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Editar Maquina"));

        jLabel1.setText("Maquinaria");

        jLabel2.setText("Estado");

        cboxEstado.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cboxEstadoActionPerformed(evt);
            }
        });

        txtKwh.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtKwhActionPerformed(evt);
            }
        });

        jLabel5.setText("Kwh");

        cboxproceso.setAutoscrolls(true);
        cboxproceso.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cboxprocesoActionPerformed(evt);
            }
        });

        jLabel6.setText("Proceso");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel5)
                    .addComponent(jLabel2)
                    .addComponent(jLabel6))
                .addGap(27, 27, 27)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(txtDescripcion, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 201, Short.MAX_VALUE)
                        .addComponent(txtKwh, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(cboxproceso, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(cboxEstado, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(26, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtDescripcion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtKwh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addGap(8, 8, 8)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cboxproceso, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cboxEstado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(0, 11, Short.MAX_VALUE))
        );

        btnGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/grabar.png"))); // NOI18N
        btnGuardar.setToolTipText("Guardar");
        btnGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGuardarActionPerformed(evt);
            }
        });

        btnCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/cancelar.png"))); // NOI18N
        btnCancelar.setToolTipText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnGuardar, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnCancelar)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(btnGuardar)
                    .addComponent(btnCancelar))
                .addGap(8, 8, 8))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtKwhActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtKwhActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtKwhActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGuardarActionPerformed
        MaquinaService tp = new MaquinaService();
        ValidacionService vs = new ValidacionService();
        Mensajes m = new Mensajes();
        
     if (vs.esVacio(txtKwh)||vs.esVacio(this.txtDescripcion)){
         JOptionPane.showInternalMessageDialog(this.getRootPane(),m.getCompletarCampos(), "Aviso", JOptionPane.WARNING_MESSAGE);
     } else{  
        if (this.cboxproceso.getSelectedIndex() - 1 == -1) {
            JOptionPane.showInternalMessageDialog(this.getRootPane(),"Ingrese un tipo de proceso", "Aviso", JOptionPane.WARNING_MESSAGE);
        }
        else { 
          try{  
            Estado est = new Estado();
            tp.editarMaquina(aux.getIdmaquina(),procesos.get(this.cboxproceso.getSelectedIndex() - 1).getIdproceso(),(String)cboxEstado.getSelectedItem(),this.txtDescripcion.getText(),this.txtKwh.getText());     
            aux.setDescripcion(this.txtDescripcion.getText());
            aux.setKwh(Float.parseFloat(this.txtKwh.getText()));
            aux.setEstado(est.getEstadoId((String)cboxEstado.getSelectedItem()));
                
            JOptionPane.showInternalMessageDialog(this.getRootPane(), m.getEditado(),"Mensaje",JOptionPane.INFORMATION_MESSAGE);
          //  ModulosT_ADM.menuMaquinaria.RefreshTable();
            
            this.dispose();
            } catch(Exception e) {
                              JOptionPane.showInternalMessageDialog(this.getRootPane(),m.getError(),"Error",JOptionPane.ERROR_MESSAGE);
             }
            
            }
        }
    }//GEN-LAST:event_btnGuardarActionPerformed

    private void cboxEstadoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cboxEstadoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cboxEstadoActionPerformed

    private void cboxprocesoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cboxprocesoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cboxprocesoActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnGuardar;
    private javax.swing.JComboBox cboxEstado;
    private javax.swing.JComboBox cboxproceso;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField txtDescripcion;
    private javax.swing.JTextField txtKwh;
    // End of variables declaration//GEN-END:variables
}
